<?php
$this->breadcrumbs=array(
    'Widgets'=>array('list'),
    'Manage',
);
?>
<h3>Manage Widgets</h2>

<style type="text/css">
.items {overflow: hidden;padding-left: 0;}
.items li a.img {
    display: table-cell; vertical-align: middle; height: 218px; width: 230px; text-align: center;
}
.items li {
    background: url(<?php echo Yii::app()->theme->baseUrl;?>/images/thumb-border-230x190.jpg) no-repeat;    
    width: 230px; height: 220px; float: left; margin: 0 10px 40px 0;
    list-style: none;
}
.items a img {border: medium none;}
.items .tools {text-align: right; padding-right: 10px; margin-top: -30px;}
.items .name {text-align: center;}
</style>
<?php
$cs = Yii::app()->getClientScript();
$gridViewCss=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('zii.widgets.assets')).'/listview/styles.css';
$cs->registerCssFile($gridViewCss);
$pagerCss=CHtml::asset(Yii::getPathOfAlias('system.web.widgets.pagers.pager').'.css');
$cs->registerCssFile($pagerCss);

echo '<p>';
echo CHtml::label('Filter Widget', 'widget_id');
echo CHtml::dropDownList('widget_id', '', $widgets, array('prompt'=>'---Select---'));
echo '</p>';
$script = "
jQuery('#widget_id').change(function(){
    jQuery('#layouts-tab').tabs('select', 0);
    var widget_id = parseInt(jQuery(this).val());
    
    //only show tabs type match widget type
    if (widget_id)
    {
        var url = '".$this->createUrl('/Core/service/ajax', array('SID'=>'Cms.widgetDefinition.get'))."';
        jQuery.get(url, {id : widget_id}, function(res){
            res = eval(res);
            if (res.model != undefined)
            {
                jQuery('span[id^=\"widget-type-\"]').each(function(){
                    if ('widget-type-'+res.model.type == jQuery(this).attr('id'))
                    {
                        jQuery(this).closest('li').removeClass('ui-tabs-hide');
                    }
                    else
                    {
                        jQuery(this).closest('li').addClass('ui-tabs-hide');
                    }
                });
            }
        });
    }
    else
    {
        jQuery('span[id^=\"widget-type-\"]').each(function(){
            jQuery(this).closest('li').removeClass('ui-tabs-hide');
        });
    }
    
    jQuery.each(jQuery('.ui-tabs-panel .templateList'), function(){
        var id = jQuery(this).attr('id');
        jQuery.fn.yiiListView.update(id, {
            data : {widget_id : widget_id}
        });
        var tab_item = jQuery('a[href=\"#create-layout\"]');
        if (tab_item.size())
        {
            var index = tab_item.closest('li').prevAll().size();
            jQuery('#layouts-tab').tabs('remove', index);
        }
        if (widget_id)
            jQuery('#layouts-tab').tabs('add', '#create-layout', 'Create Layout');
    });
});
";
$cs->registerScript(__CLASS__.'FilterByWidgetId', $script);

$domain = strstr(parse_url(Yii::app()->request->hostInfo, PHP_URL_HOST), '.');
$this->widget('zii.widgets.jui.CJuiTabs', array(
    'id'=>'layouts-tab',
    'tabs'=>$tabs,
    'options'=>array(
        'cache'=>true,
        'cookie'=>array(
            'expires'=>30,
            'domain'=>$domain,
        ),
        'select'=>'js:function(event, ui){
            var href = $(ui.tab).attr("href");
            switch (href)
            {
                case "#create-widget":
                    var index = $(ui.tab).closest("li").prevAll().size();
                    jQuery("#layouts-tab").tabs("remove", index);
                    window.location = \''.$this->createAbsoluteUrl('/Cms/admin/widget/create').'\';
                    return;
                case "#create-layout":
                    var widget_id = parseInt(jQuery("#widget_id").val());
                    window.location = \''.$this->createAbsoluteUrl('/Cms/admin/widgetLayout/create').'&widget_id=\'+widget_id;
                    return;
                case "#create-form":
                    var index = $(ui.tab).closest("li").prevAll().size();
                    jQuery("#layouts-tab").tabs("remove", index);
                    window.location = \''.$this->createAbsoluteUrl('/Cms/admin/widget/editForm').'\';
                    return;
            }
            if (jQuery(".templateList", ui.panel).size() <= 0)
            {
                var widget_id = parseInt(jQuery("#widget_id").val());
                var url = $.data(ui.tab, "load.tabs");
                url = jQuery.param.querystring(url, {widget_id : widget_id});
                jQuery("#layouts-tab").tabs( "url" , ui.index , url );
            }
        }'
    ),
));

$script = "
jQuery('body').delegate('.button-column .view-form-data', 'click', function(){
    var href = jQuery(this).attr('href');
    jQuery.fancybox({
        'href': href,
        'type': 'iframe',
        'width':'100%',
        'height':'100%'
    });
    return false;
});
";
Yii::app()->clientScript->registerScript(__CLASS__.'#ViewFormData', $script);

$cs->registerScriptFile(Yii::app()->theme->baseUrl.'/scripts/fancybox/jquery.fancybox-1.3.4.pack.js');
$cs->registerCssFile(Yii::app()->theme->baseUrl.'/scripts/fancybox/jquery.fancybox-1.3.4.css');
$baseScriptUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('zii.widgets.assets')).'/gridview';
Yii::app()->getClientScript()->registerCssFile($baseScriptUrl.'/styles.css');
$url=CHtml::asset(Yii::getPathOfAlias('system.web.widgets.pagers.pager').'.css');
Yii::app()->getClientScript()->registerCssFile($url);
$cs->registerCoreScript('jquery');
$cs->registerCoreScript('bbq');
$cs->registerScriptFile($baseScriptUrl.'/jquery.yiigridview.js',CClientScript::POS_END);